package com.example.pixelyear.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.opengl.ETC1;
import android.os.Build;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.example.pixelyear.MyApp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class UIUtils {

    /**
     * 获取屏幕尺寸
     */
    @SuppressWarnings("deprecation")
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public static Point getScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
            return new Point(display.getWidth(), display.getHeight());
        } else {
            Point point = new Point();
            display.getSize(point);
            return point;
        }
    }

    /**
     * dip转换px
     */
    public static int dip2px(Context mContext, int dip) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    /**
     * px转换dip
     */

    public static int px2dip(Context mContext, int px) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 根据string-array id 获取 数组
     */
    public static List<String> getStringArray(int id) {
        return Arrays.asList(getResource().getStringArray(id));
    }

    public static Resources getResource() {
        return getContext().getResources();
    }

    public static Context getContext() {
        return MyApp.getContext();
    }

    /**
     * 根据Layout id 获取 view 对象
     *
     * @param layout
     * @return
     */
    public static View inflate(int layout) {
        return View.inflate(getContext(), layout, null);
    }

    public static Drawable getDrawable(int id) {
        return getResource().getDrawable(id);
    }

    public static int getColor(int id) {
        return getResource().getColor(id);
    }

    public static int getDimension(int id) {
        return (int) getResource().getDimension(id);
    }

    public static float getDimensionFloat(int id) {
        return getResource().getDimension(id);
    }

    /**
     * 通过id 获取string
     *
     * @param id
     * @return
     */
    public static String getString(int id) {
        return getResource().getString(id);
    }

    /**
     * 代码中设置间距
     *
     * @param v
     * @param l
     * @param t
     * @param r
     * @param b
     */
    public static void setMargins(View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }

    /**
     * 设置状态栏颜色
     *
     * @param context
     * @param type     1.透明,2.全屏,3.填充颜色
     * @param setColor 只有为type=3，才有用
     */
    public static void setStatusBar(Activity context, int type, int setColor) {
        //总是不知道这个地方为什么老是报空指针
        try {
            Window window = context.getWindow();
            switch (type) {
                case 1://透明
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.setStatusBarColor(Color.TRANSPARENT);
                    } else {
                        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    }
                    break;
                case 2://全屏
                    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    break;
                case 3://填充颜色
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        //window.setStatusBarColor(getColor(setColor));
                        window.setStatusBarColor(context.getResources().getColor(setColor));
                    }
                    break;

            }
        } catch (NullPointerException ignored) {
        }
    }


    private static Typeface textFont = null;

    public static Typeface getTextFont(Context context) {
        if (textFont == null)
            textFont = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        return textFont;
    }

}